package cn.charmaine.org.service.impl;

import cn.charmaine.basic.Exception.CustomException;
import cn.charmaine.basic.base.query.BaseQuery;
import cn.charmaine.basic.base.service.impl.BaseServiceImpl;
import cn.charmaine.basic.util.MD5Utils;
import cn.charmaine.basic.util.PageList;
import cn.charmaine.basic.util.StrUtils;
import cn.charmaine.org.domain.Department;
import cn.charmaine.org.domain.Employee;
import cn.charmaine.org.domain.Employee;
import cn.charmaine.org.domain.Logininfo;
import cn.charmaine.org.mapper.*;
import cn.charmaine.org.mapper.IEmployeeMapper;
import cn.charmaine.org.query.EmployeeQuery;
import cn.charmaine.org.service.IEmployeeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {
    @Autowired
    private IEmployeeMapper employeeMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private IDepartmentMapper departmentMapper;

    @Override
    @Transactional
    public void add(Employee employee) {
        System.out.println(employee);
        //1、非空验证
        if (StringUtils.isEmpty(employee.getUsername()) ||
                StringUtils.isEmpty(employee.getEmail()) ||
                StringUtils.isEmpty(employee.getPhone()) ||
                StringUtils.isEmpty(employee.getPassword()) ||
                StringUtils.isEmpty(employee.getRole()) ||
                StringUtils.isEmpty(employee.getShop())
        ) {
            throw new CustomException("参数异常！");
        }
        //用工具类生成盐值
        String salt = StrUtils.getComplexRandomString(32);
        //给密码加密
        String password = MD5Utils.encrypByMd5(employee.getPassword() + salt);
        //先设置默认值
        Employee emp = dto2Employee(employee, salt, password);
        Logininfo logininfo = Employee2Logininfo(emp);
        //保存logininfo、shop、employee、t_employee_role
        //保存logininfo
        logininfoMapper.insert(logininfo);
        //保存店铺
        shopMapper.insert(employee.getShop());

        //保存员工
        emp.setDepartmentId(employee.getDepartment().getId());//设置部门
        emp.setLogininfoId(logininfo.getId());
        emp.setShopId(employee.getShop().getId());
        employeeMapper.insert(emp);
        //保存员工与角色的关联关系
        employeeMapper.insertRelation(emp.getId(), employee.getRole().getId());
        /*employeeMapper.insertRelation(emp.getId(), employee.getRoles());*/

    }

    private Logininfo Employee2Logininfo(Employee emp) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(emp, logininfo);
        logininfo.setType(0);
        logininfo.setDisable(1);
        return logininfo;
    }

    private Employee dto2Employee(Employee employee, String salt, String password) {
        Employee emp = new Employee();
        BeanUtils.copyProperties(employee, emp);
        emp.setSalt(salt);
        emp.setPassword(password);
        return emp;
    }

    /**
     * 修改
     *
     * @param employee
     */
    @Override
    public void update(Employee employee) {
        //先删除再添加
        deleteById(employee.getId());
        add(employee);
    }

    /**
     * 通过id删除
     *
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        //用id查询出当前行对象
        Employee employee = employeeMapper.selectById(id);
        //删除关联的logininfo的信息
        logininfoMapper.deleteById(employee.getLogininfoId());
        //删除中间表t_employee_role的信息
        employeeMapper.deleteRelationByEmpId(id);
        //删除员工信息
        employeeMapper.deleteById(id);
    }

    @Override
    public void batchDelete(List<Long> ids) {
        List<Long> lids = new ArrayList<>();
        for (Long id : ids) {
            Employee employee = employeeMapper.selectById(id);
            Long lid = employee.getLogininfoId();
            lids.add(lid);
        }
        System.out.println(lids);
        //删除logininfo表中的数据
        logininfoMapper.batchDelete(lids);
        employeeMapper.batchDelete(ids);
    }

    @Override
    public List<Employee> queryByShopId(Long shopId) {
        return employeeMapper.clerkList(shopId);
    }
}

